import circle.CircleLinkedList;
import single.List;
import utils.Asserts;

/**
 * @author 陈艺博
 * @date 2021-02-09 10:06
 * @description 链表测试
 **/
public class ListTest {
    public static void main(String[] args) {
//        testList(new ArrayList<>());
//        testList(new LinkedList<>());
//        testList(new SingleCircleLinkedList<>());
//        testList(new CircleLinkedList<>());
        josephus(8, 2);

    }

    /**
     * 约瑟夫问题
     * 由1开始，依次移动n次, 移动到哪里就删除该位置， 直到全部删除完毕。
     *
     * @param number     初始个数
     * @param nextNumber 移动次数
     */
    static void josephus(int number, int nextNumber) {
        CircleLinkedList<Integer> list = new CircleLinkedList<>();
        for (int i = 1; i <= number; i++) {
            list.add(i);
        }
        // 指向头结点
        list.reset();
        while (!list.isEmpty()) {
            // 向后移动 nextNumber次
            for (int i = 0; i < nextNumber; i++) {
                list.next();
            }
            // 删除指向的节点
            System.out.println(list.remove());
        }
    }

    static void testList(List<Integer> list) {
        list.add(11);
        list.add(22);
        list.add(33);
        list.add(44);

        list.add(0, 55);// [55,11,22,33,44]
        list.add(2, 66);// [55,11,66,22,33,44]
        list.add(list.size(), 77);//[55,11,66,22,33,44,77]

        list.remove(0);// [11,66,22,33,44,77]
        list.remove(2);// [11,66,33,44,77]
        list.remove(list.size() - 1);// [11,66,33,44]

        Asserts.test(list.indexOf(44) == 3);
        Asserts.test(list.indexOf(22) == list.ELEMENT_NOT_FOUND);
        Asserts.test(list.contains(33));
        Asserts.test(list.get(0) == 11);
        Asserts.test(list.get(1) == 66);
        Asserts.test(list.get(list.size() - 1) == 44);
        System.out.println(list);
    }

}
